/*
 * bluetooth.c
 *
 *  Created on: 28 Apr 2015
 *      Author: yiqi.pan
 */
#include "stm32f0xx.h"
#include "stm32f0xx_conf.h"
#include "common.h"
#include "config.h"
#include "bluetooth.h"
#include "protocol.h"
#include "uart.h"
#include "dma.h"
#include "rb112.h"
#include <stdint.h>
#include <string.h>

uint8_t TxBuffer[BT_DATA_TO_TRANSFER] = {0};
uint8_t RxBuffer[BT_DATA_TO_RECEIVE] = {0};
volatile uint8_t rxpos;
volatile uint8_t received_packet_len = 0;
volatile uint8_t new_packet_received = 0;


volatile bool tx_sending = false;

/* varialbles */
static bool bt_connected;
static uint8_t conn_mode;

/* dma 0/1 interrupt handler */
void DMA1_Channel2_3_IRQHandler(void)
{
    /* Test on DMA Stream Transfer Complete interrupt */
    if (DMA_GetITStatus(DMA1_IT_TC2))
    {
        /* Clear DMA Stream Transfer Complete interrupt pending bit */
        DMA_ClearITPendingBit(DMA1_IT_TC2);

        tx_sending = false;
    }
}

/* uart1 interrupt handler */
void uart1_rx_interrupt(void)
{
	/*
	uint8_t packetlen;

	RxBuffer[rxpos] = USART_ReceiveData8(BT_UART);

	if(rxpos < PACKET_HEADER_LEN)
		return;

	if( rxpos >= RxBuffer[PACKET_HEADER_LEN - 1] + PACKET_FRAME_LEN)
	{
		uart_disable_rx(BT_UART);
		received_packet_len = rxpos;
		new_packet_received = 1;
	}*/
	
	RxBuffer[rxpos++] = USART_ReceiveData(BT_UART);	
	if(rxpos==BT_DATA_TO_RECEIVE)
		rxpos--;

	if(rb_receive_handler(RxBuffer, rxpos)>0)
		bt_uart_reset_rx_buffer();
}

/* Initialise Bluetooth UART, refer to 
 */ 
void bt_uart_init(void)
{
    USART_InitTypeDef USART_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    DMA_InitTypeDef  DMA_InitStructure;
         
    /* Configure USART GPIO GPIO Alternate functions */
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
     
    /* Configure USART1 pins:  Rx and Tx ----------------------------*/
    GPIO_InitStructure.GPIO_Pin =  BT_UART_RxPin | BT_UART_TxPin;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
   
    /* Enable USART1  and DMA channnel IRQ */
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn|DMA1_Channel2_3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    
    
    /* USART Configuration */    
    USART_InitStructure.USART_BaudRate = BT_UART_BAUDRATE;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_Init(BT_UART, &USART_InitStructure);
    
    /* Enable UART Interrupt */
    USART_ITConfig(BT_UART, USART_IT_RXNE, ENABLE);
    
    USART_Cmd(BT_UART,ENABLE);
  
    
    /* USART TX DMA Channel Config */
    DMA_DeInit(BT_USART_DMA_CHANNEL_TX);
    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)BT_UART->TDR;
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)TxBuffer;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
    DMA_InitStructure.DMA_BufferSize = BT_DATA_TO_TRANSFER;
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
    DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
    DMA_InitStructure.DMA_Priority = DMA_Priority_High;
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
    
    DMA_Init(BT_USART_DMA_CHANNEL_TX, &DMA_InitStructure);
      
    /* Enable the USART Tx DMA request */
    USART_DMACmd(BT_UART, USART_DMAReq_Tx, ENABLE);
    
    /* Enable DMA Stream Half Transfer and Transfer Complete interrupt */
    DMA_ITConfig(BT_USART_DMA_CHANNEL_TX, DMA_IT_TC, ENABLE);
    
    /* DMA1 Channel1 enable */
    DMA_Cmd(BT_USART_DMA_CHANNEL_TX, ENABLE);

}

void bt_uart_reset_tx_buffer(void) 
{

}

void bt_uart_reset_rx_buffer(void)
{
	//memset(RxBuffer, 0 ,48);
	rxpos = 0;
}
void bt_uart_close(void)
{
	/* Disable the USART Tx DMA channel */
	DMA_Cmd(BT_USART_DMA_CHANNEL_TX, DISABLE);

	DMA_DeInit(BT_USART_DMA_CHANNEL_TX);

	USART_Cmd(BT_UART, DISABLE);
}


void bt_uart_send(uint8_t* packet, uint8_t packetlen)
{
	while(tx_sending);
	//while(DMA_GetCurrDataCounter(BT_USART_DMA_CHANNEL_TX)>0);
	
	memcpy(TxBuffer, packet, packetlen);
		
	DMA_Cmd(BT_USART_DMA_CHANNEL_TX, DISABLE);

	DMA_SetCurrDataCounter(BT_USART_DMA_CHANNEL_TX, packetlen);
	
	tx_sending = true;

	/* Disable the USART Tx DMA channel */
	DMA_Cmd(BT_USART_DMA_CHANNEL_TX, ENABLE);
}


uint8_t bt_get_state(void)
{
	return 0;
}

void bt_worker(void)
{
	if(new_packet_received)
	{
		parse_packet(RxBuffer, received_packet_len);
		bt_uart_reset_tx_buffer();
		uart_enable_rx(BT_UART); //enable receiver
	}
}

void bt_manager(void)
{
	if(bt_connected)
	{

	}
	else
	{

	}
}

